<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title></title>
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">

		<!--标准mui.css-->
		<link rel="stylesheet" href="../css/mui.min.css">
		<!--App自定义的css-->
		<link rel="stylesheet" type="text/css" href="../css/app.css" />
		<style>
		</style>
	</head>

	<body>
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title">更新版本</h1>
		</header>
		<div class="mui-content">
			<form id="form">
				<div class="mui-input-group">
					<div class="mui-input-row">
						<label>当前版本:</label><label id="version" style="width: 50%;">0.0</label>
					</div>
				</div>
				<div class="mui-input-group">
					<div class="mui-input-row">
						<label>新版本号:</label>
						<input type="text" class="mui-input-clear" placeholder="新版本号" id="title" name="version">
					</div>
				</div>
				<div class="mui-input-group">
					<div class="mui-input-row">
						<label>更新文件:</label>
						<input type="file" id="file" name="file"  style="padding-top: 1%;">
					</div>
				</div>
				<div class="mui-input-group">
					<div class="mui-input-row">
						<label>更新类型:</label>
						<select id="type" name="type">
							<option value="0">整包(apk)</option>
							<option value="1">资源(wgt)</option>
						</select>
					</div>
				</div>
				<div class="mui-input-group">
					<div class="mui-input-row">
						<label>强制更新:</label>
						<select id="require" name="require">
							<option value="0">可选</option>
							<option value="1">强制</option>
						</select>
					</div>
				</div>
				<div class="mui-input-group">
					<textarea id="content" style="width: 100%;min-height: 6rem;" name="updateNotice" placeholder="更新说明"></textarea>
				</div>
				<div class="mui-input-row" style="height: 100%;padding: 1%;">
					<button class="mui-btn mui-btn-primary" type="button" style="width: 100%;" id="sub">提交</button>
				</div>
			</form>
		</div>

	</body>
	<script src="../js/mui.min.js"></script>
	<script src="../js/config.js"></script>
	<script>
		var loading = config.config.loading;
		mui.init({

		});
		mui.plusReady(function() {
			plus.nativeUI.showWaiting(loading);
			init();
		});

		function init() {
			mui.ajax(config.config.path + "/app/checkUpdate", {
					type: "GET",
					success: function(result) {
						if(result.code == 200){
							document.getElementById("version").innerHTML = result.data.version;
						}else{
							mui.toast(result.message, {
								duration: 'long',
								type: 'div'
							});
						}
						plus.nativeUI.closeWaiting();
					},
					error: function(e) {
						plus.nativeUI.closeWaiting();
						if (e.status == 500 || e.status == 404) {
							mui.toast('服务器错误', {
								duration: 'long',
								type: 'div'
							});
						} else if (e.status == 0) {
							mui.toast('连接服务器失败，请检查网络连接', {
								duration: 'long',
								type: 'div'
							});
						}
					}
				});
		}

		document.getElementById("sub").addEventListener('tap', function() {
			document.getElementById('sub').disabled = 'disabled';
			var title = document.getElementById("title").value;
			if(title == ''){
				mui.alert('请输入版本号','提示','知道了',null,'div');
				document.getElementById('sub').disabled = '';
				return;
			}
			if(document.getElementById('file').value == ''){
				mui.alert('请选择更新包文件！','提示','知道了',null,'div');
				document.getElementById('sub').disabled = '';
				return;
			}
			if(document.getElementById('type').value == ''){
				mui.alert('请选择更新包类型！','提示','知道了',null,'div');
				document.getElementById('sub').disabled = '';
				return;
			}
			if(document.getElementById('require').value == ''){
				mui.alert('请选择更新类型！','提示','知道了',null,'div');
				document.getElementById('sub').disabled = '';
				return;
			}
			if(document.getElementById('content').value == ''){
				mui.alert('请输入更新说明','提示','知道了',null,'div');
				document.getElementById('sub').disabled = '';
				return;
			}
			
			mui.confirm('确认保存吗？', '提示', ['确认', '取消'], function(e) {
				if (e.index == 0) {
					plus.nativeUI.showWaiting(loading);
					var formData = new FormData(document.getElementById('form'));
					mui.ajax(config.config.path + '/app/updateVersion',{
						type: "POST",
						data: formData,
						contentType: false,
						cache: false,
						processData: false,
						success: function(result) {
							mui.alert(result,'提示','好的',null,'div');
							init();
							document.getElementById('title').value = '';
							document.getElementById('file').value = '';
							document.getElementById('type').value = '';
							document.getElementById('require').value = '';
							document.getElementById('content').value = '';
							document.getElementById('sub').disabled = '';
							plus.nativeUI.closeWaiting();
						},
						error: function(e){
							console.log("e: " + JSON.stringify(e));
							plus.nativeUI.closeWaiting();
							if(e.status == 500 || e.status == 404){
								mui.toast('服务器错误', {
									duration: 'long',
									type: 'div'
								});
							}else if(e.status == 0){
								mui.toast('连接服务器失败，请检查网络连接', {
									duration: 'long',
									type: 'div'
								});
							}
						}
					});
				}
			}, 'div');
			
		})
	</script>
</html>
